<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
    require_once("class.connectSQL.inc");
    
    //instancia da classe
    $banco = new connectSQL();
	$banco1 = new connectSQL();
	$banco2 = new connectSQL();
	$banco3 = new connectSQL();
	$banco4 = new connectSQL();
	$banco5 = new connectSQL();
	
	
	if(isset($_POST["acao"])){
		
		if($_POST["acao"]=="I"){ 
			$dados = array(
							"codNotaFiscal"  => $_POST["codNotaFiscal"],
							"codFornec"  => $_POST["codFornec"],
							"valNota"  => $_POST["valNota"]
							);
			
			$banco->Insert("estnota", $dados);
			//die($_POST["acao"]);
			$msg = "Incluido com sucesso";
		}
		if($_POST["acao"]=="A"){ 
			$sql = "UPDATE estnota set codNotaFiscal = '".$_POST["codNotaFiscal"]."',
										codFornec = '".$_POST["codFornec"]."',
										valNota = '".$_POST["valNota"]."'
										where codNota= ".$_POST["codNota"]." ";
			//die($sql);
			
			$banco3->Generico($sql);
			//die($_POST["acao"]);
			$msg = "Alterado com sucesso";
		}
		
		}
		
		if(isset($_GET["excluir"])){
				$sql = "Delete from estnota where codNota= ".$_GET["cod"]." ";
				//die($sql);
				
				$banco3->Generico($sql);
				//die($_POST["acao"]);
				$msg = "Excluido com sucesso";
			}
?>

	<head>
	<?php include "head.php"; ?> 
    <script>   
    function validar(form){
    if(form.codNotaFiscal.value == ''){
        alert("O campo Nota Fiscal é obrigatório.");
        return false;
    }
	if(form.valNota.value == ''){
        alert("O campo Valor da Nota é obrigatório.");
        return false;
    }
   
    return true;
	}
  </script>    
	</head>
	<body>
    	<?php include "sair.php"; ?>
            
            <!-- Header. Main part -->
            <div id="header-main">
                <div class="container_12">
                    <div class="grid_12">
                        <div id="logo">
                            <ul id="nav">
                                <li id="current"><a href="">Nota Fiscal</a></li>
                                
                            </ul>
                        </div><!-- End. #Logo -->
                    </div><!-- End. .grid_12-->
                    <div style="clear: both;"></div>
                </div><!-- End. .container_12 -->
            </div> <!-- End #header-main -->
            <div style="clear: both;"></div>
            <!-- Sub navigation -->
          
        </div> <!-- End #header -->
        
		<div class="container_12" align="center" >
        

            <div class="module">
                     <h2><span>Cadastro de Nota Fiscal</span></h2>
                        
                     <div class="module-body">
                        <form action="cadnota.php" onsubmit="return validar(this);" id="form" method="post">
                        
                            
                           
                            <p align="left">
                            <?php
					 		$cod = $banco1->GerarCod("codNota","estnota");
							if($cod < 1){
								$cod = 1;
								}
							?>
                            
                                <label>Código Nota (Interno)</label>
                                <label><?php  if(isset($_GET["altera"])){echo $_GET["cod"];}else{echo $cod;} ?></label>
                                <input type="hidden" name="codNota" id="codNota" value="<?php  if(isset($_GET["altera"])){echo $_GET["cod"];}else{echo $cod;} ?>" />
                                
                            </p>
                            
                            <?php
					 
					 	 if(isset($_GET["altera"])){
							 $resultado = $banco4->Query("SELECT * FROM estnota where codNota = '".$_GET["cod"]."'");
							 foreach ($resultado as $resut){
								$desc =  $resut['CodNotaFiscal'];
								$desc1 =  $resut['codFornec'];
								$desc2 =  $resut['valNota'];
							 }
						 }else{
							 	$desc = "";
								$desc1 = "";
								$desc2 = "";
						  } 
				?>
                            
                            <p align="left">
                                <label>Nota Fiscal</label>
                                <input type="text" class="input-medium" id="codNotaFiscal" name="codNotaFiscal" value="<?php echo $desc; ?>" />
                                
                            </p>
                             <p align="left">	
                                <label>Fornecedor</label>
                                <select class="input-medium" name="codFornec" id="codFornec">
                                <option value="">Selecione um Fornecedor</option>
                                 <?php
										$resultado = $banco->Query("SELECT * FROM estfornec");
										foreach ($resultado as $resut){
											$codFornec = $resut['codFornec'];
											if($desc1 == $codFornec ){
												echo "<option value='".$codFornec."' selected>".$resut['desForn']."</option>";
											}else{
												echo "<option value=".$codFornec." >".$resut['desForn']."</option>";
											}
										
										}
		?>
                               </select>
                            </p>
                            <p align="left">
                                <label>Valor</label>
                                <input type="text" class="input-medium" id="valNota" name="valNota" value="<?php echo $desc2; ?>"  />
                            </p>
                           <input type="hidden" name="acao" value="<?php if(isset($_GET["altera"])){echo "A";}else{echo "I";} ?>" />
                            <fieldset>
                                <input class="submit-green" type="submit" value="Salvar" /> 
                                <input class="submit-gray" type="reset" value="Cancelar" />
                            </fieldset>
                        </form>
                     </div> 
                      <!-- End .module-body -->
					 
                </div>  <!-- End .module -->
				<?php 
				if(isset($msg)){
				echo "<span class='notification n-success'>".$msg."</span>";
				}
				?>
				
				                     
        
                <!-- Example table -->
                <div class="module">
                	<h2>Notas Fiscais</h2>
                    
                    <div class="module-table-body">
                    	<form action="">
                        <table id="myTable" class="tablesorter">
                        	
                                <tr>
                                    <th width="9%" style="width:5%">#</th>
                                    <th width="24%" style="width:10%">Nota</th>
                                    <th width="24%" style="width:20%">Fornecedor</th>
                                    <th width="9%" style="width:10%">Valor</th>
                                    <th width="34%" style="width:35%"></th>
                                </tr>
                            
                            <tbody>
                            <?php
					 $resultado = $banco5->Query("SELECT A.codNota, A.codFornec, A.CodNotaFiscal, A.valNota, (
													SELECT COUNT( B.codNota ) 
													FROM estlote AS B
													WHERE B.codNota = A.codNota
													) AS lote, (
													SELECT desForn 
													FROM estfornec AS B
													WHERE B.codFornec = A.codFornec
													) AS fornec
													FROM estnota AS A ORDER BY A.codNota DESC LIMIT 0 , 20");
			
    
        foreach ($resultado as $resut){
        echo "<tr>";
		echo "<td>". $resut['codNota']."</td>";
		echo "<td>". $resut['CodNotaFiscal']."</td>";
		echo "<td>". $resut['fornec']."</td>";
		echo "<td> R$ ". $resut['valNota']."</td>";
		?>
        <td>
        <a href="cadlote.php?codNota=<?php echo $resut['codNota']; ?>"><img src="lote.png"  width="30" height="30" alt="edit" /></a>
        <a href="cadnota.php?altera=1&cod=<?php echo $resut['codNota']; ?>"><img src="pencil.gif" tppabs="http://www.xooom.pl/work/magicadmin/images/pencil.gif" width="30" height="30" alt="edit" /></a>
        <?php if($resut['lote'] == 0){?>
        <a href="cadnota.php?excluir=1&cod=<?php echo $resut['codNota']; ?>"><img src="bin.gif" tppabs="http://www.xooom.pl/work/magicadmin/images/bin.gif" width="30" height="30" alt="delete" /></a>
        <?php  
		}else{
			echo "&nbsp;&nbsp; Essa Nota possui Lotes vinculados!";
			
			}
			
  		echo "</tr>";
		}
   		                  ?>  
                                
                                             
                     
                     
                      </tbody>
                        </table>
                        </form>
                     
                     
                 </div>    
                     
                     
            <div style="clear:both;"></div>
            
            <div style="clear:both;"></div>
        </div> <!-- End .container_12 -->
		
           
        <!-- Footer -->
       <?php include "footer.php"; ?>
	</body>
</html>